package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.CategoryBrandService;
import com.atguigu.spzx.model.dto.product.CategoryBrandDto;
import com.atguigu.spzx.model.entity.product.Brand;
import com.atguigu.spzx.model.entity.product.CategoryBrand;
import com.atguigu.spzx.model.vo.common.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product/categoryBrand")
@CrossOrigin(allowCredentials = "true" , originPatterns = "*" , allowedHeaders = "*")

public class CategoryBrandController {

    @Autowired
    private CategoryBrandService categoryBrandService;

    @GetMapping("/{page}/{limit}")
    public Result<PageInfo<CategoryBrand>> getCategoryBrandPageList(
            @PathVariable Integer page,@PathVariable Integer limit,CategoryBrandDto searchObj
    ){
        PageInfo<CategoryBrand> pageInfo = categoryBrandService.getCategoryBrandPageList(page,limit,searchObj);
        return Result.ok(pageInfo);
    }

    //CategoryBrand 前端只传入了brandId  categoryId
    @PostMapping("/save")
    public Result save(@RequestBody CategoryBrand categoryBrand){
        categoryBrandService.save(categoryBrand);
        return Result.ok(null);
    }

    //CategoryBrand 前端只传入了brandId(可能为空)  categoryId(可能为空)  被修改数据的主键id
    @PutMapping("/updateById")
    public Result updateById(@RequestBody CategoryBrand categoryBrand){
        categoryBrandService.updateById(categoryBrand);
        return Result.ok(null);
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id){
        categoryBrandService.deleteById(id);
        return Result.ok(null);
    }

    @GetMapping("/findBrandByCategoryId/{categoryId}")
    public Result<List<Brand>> findBrandByCategoryId(@PathVariable Long categoryId){
        List<Brand> brandList = categoryBrandService.findBrandByCategoryId(categoryId);
        return Result.ok(brandList);
    }
}
